package com.ruoyi.web.api.controller.system;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.AppUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.SysHealthData;
import com.ruoyi.system.domain.qo.DateRangeQo;
import com.ruoyi.system.domain.qo.HealthDataQo;
import com.ruoyi.system.domain.vo.HealthDataVo;
import com.ruoyi.system.domain.vo.HeartRateIndexVo;
import com.ruoyi.system.service.ISysHealthDataService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 健康数据Controller
 *
 * @author ruoyi
 */
@Api(tags = "健康数据管理")
@RestController
@RequestMapping("/healthData")
public class SysHealthDataController extends BaseController {
    private final ISysHealthDataService sysHealthDataService;

    public SysHealthDataController(ISysHealthDataService sysHealthDataService) {
        this.sysHealthDataService = sysHealthDataService;
    }

    /**
     * 查询健康数据列表
     */
    @ApiOperation("查询健康数据列表")
    @GetMapping("/page")
    public TableDataInfo page(SysHealthData sysHealthData) {
        startPage();
        List<SysHealthData> list = sysHealthDataService.selectSysHealthDataList(sysHealthData);
        return getDataTable(list);
    }

    /**
     * 查询健康数据列表
     */
    @ApiOperation("查询健康数据列表")
    @GetMapping("/list")
    public TableDataInfo list(HealthDataQo healthDataQo) {
        List<HealthDataVo> list = sysHealthDataService.selectHealthDataList(healthDataQo);
        return getDataTable(list);
    }

    /**
     * 查询健康数据列表
     */
    @ApiOperation("查询健康数据列表")
    @GetMapping("/myList")
    public TableDataInfo myList(HealthDataQo healthDataQo) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        healthDataQo.setUserId(loginUser.getUserId());
        List<HealthDataVo> list = sysHealthDataService.selectHealthDataList(healthDataQo);
        return getDataTable(list);
    }

    /**
     * 获取健康数据详细信息
     */
    @ApiOperation("获取健康数据详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@ApiParam("健康数据ID") @PathVariable("id") Long id) {
        return success(sysHealthDataService.selectSysHealthDataById(id));
    }

    /**
     * 根据设备ID查询最新健康数据
     */
    @ApiOperation("根据设备ID查询最新健康数据")
    @GetMapping("/latest/device/{deviceId}")
    public AjaxResult getLatestByDeviceId(@ApiParam("设备ID") @PathVariable("deviceId") String deviceId) {
        List<SysHealthData> list = sysHealthDataService.selectLatestByDeviceId(deviceId);
        if (!list.isEmpty()) {
            return success(list.get(0));
        } else {
            return success(null);
        }
    }

    /**
     * 查询健康数据列表
     */
    @ApiOperation("查询心率指标")
    @GetMapping("/heartRateIndex")
    public AjaxResult heartRateIndex(DateRangeQo dateRangeQo) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        AppUser user = loginUser.getAppUser();
        Long userId = user.getId();
        HealthDataQo healthDataQo = new HealthDataQo();
        healthDataQo.setUserId(userId);
        healthDataQo.setStartTime(dateRangeQo.getStartTime());
        healthDataQo.setEndTime(dateRangeQo.getEndTime());
        HeartRateIndexVo indexVo = sysHealthDataService.getHeartRateIndex(healthDataQo);
        return success(indexVo);
    }


    /**
     * 根据用户ID查询最新健康数据
     */
    @ApiOperation("根据用户ID查询最新健康数据")
    @GetMapping("/latest/user")
    public AjaxResult getLatestByUserId() {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        AppUser user = loginUser.getAppUser();
        Long userId = user.getId();
        List<SysHealthData> list = sysHealthDataService.selectLatestByUserId(userId);
        if (!list.isEmpty()) {
            return success(list.get(0));
        } else {
            return success(null);
        }
    }
}